package com.sudo.service.res.service.impl;

import com.github.pagehelper.PageHelper;
import com.sudo.common.utils.PagedGridResult;
import com.sudo.common.utils.ProvinceCityUtil;
import com.sudo.dao.res.entity.ResProvinceCity;
import com.sudo.dao.res.mapper.ResProvinceCityMapper;
import com.sudo.dao.res.model.bo.ResProvinceCityBo;
import com.sudo.service.admin.service.AdminBaseService;
import com.sudo.service.res.service.ResProvinceCityService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class ResProvinceCityServiceImpl extends AdminBaseService implements ResProvinceCityService {

    @Resource
    private ResProvinceCityMapper resProvinceCityMapper;

    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    @Override
    public void refreshData(List<ProvinceCityUtil.SysCity> sysAreas) {
        if (CollectionUtils.isNotEmpty(sysAreas)) {
            resProvinceCityMapper.truncateTable();
            LocalDateTime now = LocalDateTime.now();
            List<ResProvinceCity> resProvinceCityList = new ArrayList<>();
            for (ProvinceCityUtil.SysCity sysArea : sysAreas) {
                ResProvinceCity resProvinceCity = new ResProvinceCity();
                BeanUtils.copyProperties(sysArea,resProvinceCity);
                resProvinceCity.setCreateTime(now);
                resProvinceCityList.add(resProvinceCity);
            }
            resProvinceCityMapper.insertList(resProvinceCityList);
        }
    }

    @Override
    public PagedGridResult queryResProvinceCityByPage(ResProvinceCityBo resProvinceCityBo) {
        PageHelper.startPage(resProvinceCityBo.getPage(), resProvinceCityBo.getPageSize());
        Example example = new Example(ResProvinceCity.class);
        example.orderBy("id").desc();
        Example.Criteria criteria = example.createCriteria();

        // 只有province：fatherCode = province
        // 有province city：fatherCode = city
        if (StringUtils.isNotBlank(resProvinceCityBo.getProvince())) {
            if (StringUtils.isNotBlank(resProvinceCityBo.getCity())){
                criteria.andEqualTo("fatherCode",resProvinceCityBo.getCity());
            }else {
                criteria.andEqualTo("fatherCode",resProvinceCityBo.getProvince());
            }
        }
        List<ResProvinceCity> resProvinceCityList = resProvinceCityMapper.selectByExample(example);
        return setterPagedGrid(resProvinceCityList, resProvinceCityBo.getPage());
    }

    @Override
    public List<ResProvinceCity> queryAddrByFatherCode(String fatherCode) {
        Example example = new Example(ResProvinceCity.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(fatherCode)) {
            criteria.andEqualTo("fatherCode", fatherCode);
        }
        return resProvinceCityMapper.selectByExample(example);
    }
}
